package com.demo.bean.service.impl.contact;

import com.demo.bean.mapper.contact.BranchCompanyMapper;
import com.demo.bean.service.contact.BranchCompanyService;
import com.demo.common.entity.BranchCompany;
import com.demo.common.entity.BranchCompanyDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分公司
 * 作者：张兴然
 */
@Service
public class BranchCompanyServiceImpl implements BranchCompanyService {
    @Autowired
    private BranchCompanyMapper branchCompanyMapper;

    @Override
    public List<BranchCompanyDTO> selectCompanyNameAndAddress(Integer pageNum, Integer pageSize) {
        //如果当前页面小于1 则重新赋值1
        if(pageNum < 1 ){
            pageNum = 1;
        }
        List<BranchCompanyDTO> branchCompanies = branchCompanyMapper.selectCompanyNameAndAddress((pageNum - 1) * pageSize, pageSize);
        for (int i = 0; i < branchCompanies.size(); i++) {
            String[] split = branchCompanies.get(i).getAddress().get(0).getCompanyAddress().split(",");
            if(split.length>1){
                branchCompanies.get(i).getAddress().get(0).setCompanyAddress(split[0]);
                for (int i1 = 1; i1 < split.length; i1++) {
                    BranchCompany branchCompany = new BranchCompany();
                    branchCompany.setCompanyAddress(split[i1]);
                    branchCompany.setTelNumber(branchCompanies.get(i).getAddress().get(0).getTelNumber());
                    branchCompanies.get(i).getAddress().add(branchCompany);
                }
            }
        }
        return branchCompanies;
    }

    @Override
    public List<BranchCompanyDTO> selectCompanyNameAndAddressBySize(Integer pageSize) {
        List<BranchCompanyDTO> branchCompanies = branchCompanyMapper.selectCompanyNameAndAddressBySize(pageSize);
        for (int i = 0; i < branchCompanies.size(); i++) {
            String[] split = branchCompanies.get(i).getAddress().get(0).getCompanyAddress().split(",");
            if(split.length>1){
                branchCompanies.get(i).getAddress().get(0).setCompanyAddress(split[0]);
                for (int i1 = 1; i1 < split.length; i1++) {
                    BranchCompany branchCompany = new BranchCompany();
                    branchCompany.setCompanyAddress(split[i1]);
                    branchCompany.setTelNumber(branchCompanies.get(i).getAddress().get(0).getTelNumber());
                    branchCompanies.get(i).getAddress().add(branchCompany);
                }
            }
        }
        return branchCompanies;
    }
}
